package com.jjb.www.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.hjq.bar.TitleBar;
import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.shape.layout.ShapeLinearLayout;
import com.hjq.shape.view.ShapeTextView;
import com.jjb.www.R;
import com.jjb.www.app.AppActivity;
import com.jjb.www.app.AppAdapter;
import com.jjb.www.bean.PostBean;
import com.jjb.www.http.api.TeachPlanListApi;
import com.jjb.www.http.model.HttpList;
import com.jjb.www.http.model.HttpList;
import com.jjb.www.ui.adapter.SigUpAdapter;
import com.jjb.www.ui.dialog.ChooseReasonDialog;
import com.jjb.www.ui.dialog.MessageDialog;
import com.jjb.www.widget.CommonListRefreshView;
import com.jjb.www.widget.CommonRefreshView;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 *    author : Android CRG
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/10/18
 *    desc   : 继续教育报名 岗前教育报名
 */
public class SignUpActivity extends AppActivity implements BaseAdapter.OnItemClickListener {
    private ShapeLinearLayout mLlMessage;
    private ShapeTextView mTvName;



    private SigUpAdapter mAdapter;
    private TitleBar mTitleBar;
    private Integer mPersion = 0;

    private CommonListRefreshView mRefreshView;
    //岗位id
    private String train_type = "1";



    @Override
    public void onTitleClick(View view) {
        List<PostBean> planList = new ArrayList<>();
        PostBean postBean1 = new PostBean();
        postBean1.setName("专业人员");
        postBean1.setPid(1);
        planList.add(postBean1);

        PostBean postBean2 = new PostBean();
        postBean2.setPid(2);
        postBean2.setName("特种作业人员");
        planList.add(postBean2);

        PostBean postBean3 = new PostBean();
        postBean3.setName("检测人员");
        postBean3.setPid(4);
        planList.add(postBean3);

        PostBean postBean4 = new PostBean();
        postBean4.setPid(5);
        postBean4.setName("监理人员");
        planList.add(postBean4);

        PostBean postBean5 = new PostBean();
        postBean5.setName("安管人员");
        postBean5.setPid(6);
        planList.add(postBean5);
        new ChooseReasonDialog.Builder(getContext())
                .setList(planList,mPersion)
                .setListener(new ChooseReasonDialog.OnListener() {
                    @Override
                    public void onCompleted(BaseDialog dialog, PostBean content,Integer persion) {
                        if (content.getName().equals("专业人员")||content.getName().equals("检测人员")||content.getName().equals("监理人员")){
                            mLlMessage.setVisibility(View.GONE);
                        }else if (content.getName().equals("特种作业人员")){
                            mLlMessage.setVisibility(View.VISIBLE);
                            mTvName.setText("请联系开课机构报名培训");
                        }else if (content.getName().equals("安管人员")){
                            mLlMessage.setVisibility(View.VISIBLE);
                            mTvName.setText("请您前往渝快办报名");
                        }
                        mPersion = persion;
                        mTitleBar.setTitle(content.getName());
                        train_type = content.getPid()+"";
                        getListData();
                    }
                }).show();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sign_up;
    }

    @Override
    protected void initView() {
        mAdapter = new SigUpAdapter(getContext());
        mTitleBar = findViewById(R.id.titleBar);
        mLlMessage = findViewById(R.id.ll_message);
        mTvName = findViewById(R.id.tv_name);
        mRefreshView = findViewById(R.id.refreshView);
        mAdapter.setOnItemClickListener(this);
        setOnClickListener(mLlMessage);
    }

    @Override
    public void onClick(View view) {
        if (view==mLlMessage){
            if (mTitleBar.getTitle().equals("安管人员")){
                new MessageDialog.Builder(getContext())
                        .setMessage("安管人员继续教育需要到渝快办报名，且仅有建筑业资质的企业登录办理，请联系您的企业为您报名。")
                        .setConfirm("我知道了")
                        .setCancel(null)
                        .setCancelable(false)
                        .setListener(new MessageDialog.OnListener() {

                            @Override
                            public void onConfirm(BaseDialog dialog) {
                            }

                            @Override
                            public void onCancel(BaseDialog dialog) {
                            }
                        })
                        .show();
            }

        }
    }

    @Override
    protected void initData() {
        getListData();
    }
    private void getListData() {
        mRefreshView.setDataHelper(new CommonListRefreshView.DataHelper<TeachPlanListApi.Bean>() {
            @Override
            public AppAdapter<TeachPlanListApi.Bean> getAdapter() {
                return mAdapter;
            }

            @Override
            public void loadData(int p, HttpCallback<HttpList<TeachPlanListApi.Bean>> callback) {
                EasyHttp.get(SignUpActivity.this)
                        .api(new TeachPlanListApi()
                                .setTrain_type(train_type)
                                .setPage(p)
                                .setPageSize(mAdapter.getSize())
                        ).request(new HttpCallback<HttpList<TeachPlanListApi.Bean>>(SignUpActivity.this) {

                    @Override
                    public void onStart(Call call) {

                    }

                    @Override
                    public void onSucceed(HttpList<TeachPlanListApi.Bean> result) {
                        callback.onSucceed(result);
                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                        callback.onFail(e);
                    }

                    @Override
                    public void onEnd(Call call) {
                        super.onEnd(call);
                        callback.onEnd(call);
                    }
                });
            }

            @Override
            public void onRefreshSuccess(List<TeachPlanListApi.Bean> list, int listCount) {

            }

            @Override
            public void onRefreshFailure() {

            }

            @Override
            public void onLoadMoreSuccess(List<TeachPlanListApi.Bean> loadItemList, int loadItemCount) {

            }

            @Override
            public void onLoadMoreFailure() {

            }
        });
        mRefreshView.initData();
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        Intent intent = new Intent();
        intent.putExtra("plan_type","1");
        intent.putExtra("className",mAdapter.getItem(position).getClass_time());
        intent.putExtra("id",mAdapter.getItem(position).getId()+"");
        intent.setClass(getContext(),PlanDetitleActivity.class);
        startActivity(intent);
    }
}